<%@ page language="java" contentType="text/html; charset=UTF-8"
         import="com.baidu.ueditor.ActionEnter"
         pageEncoding="UTF-8" %>
<%@ page import="com.qiniu.storage.model.DefaultPutRet" %>
<%@ page import="com.rybbaby.tss.core.utils.*" %>
<%@ page import="net.sf.json.JSONObject" %>
<%@ page import="org.apache.commons.io.IOUtils" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page trimDirectiveWhitespaces="true" %>
<%!
    private void uploadFile(HttpServletRequest requst, String rootPath, String exec, final JspWriter out) throws IOException {
        JSONObject jsonObject = JSONObject.fromObject(exec);
        String url = JSONUtil.getStringVal(jsonObject, "url");
        String suffix = JSONUtil.getStringVal(jsonObject, "type");
        String type = suffix;
        rootPath=rootPath.replaceAll("\\\\","/");
        System.out.println("------------" + rootPath + "/" + url);
        InputStream inputStream = new FileInputStream(rootPath + url);
        String newFileName = SequenceUtil.getUpperCaseUUID();
        File tempFile = File.createTempFile(newFileName, suffix);
        final Long size = tempFile.length();
        FileUtils.copyInputStreamToFile(inputStream, tempFile);
        IOUtils.closeQuietly(inputStream);
        DefaultPutRet ret = QiNiuUtil.upload(tempFile);
        if (ret != null) {
            Map<String, String> result = new HashMap<>();
            String qiniuKey = ret.key;
            String hash = ret.hash;
            result.put("url", qiniuKey);
            result.put("state", "SUCCESS");
            result.put("type", "jpg");
            result.put("size", size + "");
            result.put("original", qiniuKey);
            try {
                out.write(JSONUtil.toJsonStr(result));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
%>
<%

    request.setCharacterEncoding("utf-8");
    response.setHeader("Content-Type", "text/html");

    String action = request.getParameter("action");
    String rootPath = application.getRealPath("/");
    String exec = new ActionEnter(request, rootPath).exec();

    if ("uploadimage".equals(action)) {
        try {
            uploadFile(request, rootPath, exec, out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        out.write(exec);
    }
%>